package Bill;

import java.util.*;
import java.text.SimpleDateFormat;
import java.text.ParseException;
class BillManager {
    private List<BillRecord> incomeRecords = new ArrayList<>();
    private List<BillRecord> expenseRecords = new ArrayList<>();
    private double monthlyBudget;
    private double totalExpenses;

    public BillManager(double monthlyBudget) {
        this.monthlyBudget = monthlyBudget;
        this.totalExpenses = 0;
    }

    public void addIncomeRecord(BillRecord record) {
        if (record.amount > 0) {
            incomeRecords.add(record);
            System.out.println("收入已成功记录！");
        } else {
            System.out.println("收入金额必须为正数！");
        }
    }

    public void addExpenseRecord(BillRecord record) {
        if (record.amount > 0) {
            expenseRecords.add(record);
            totalExpenses += record.amount;
            System.out.println("支出已成功记录！");
        } else {
            System.out.println("支出金额必须为正数！");
        }
    }

    public void displayRecords(List<BillRecord> records) {
        if (records.isEmpty()) {
            System.out.println("没有找到相关账单记录。");
            return;
        }
        for (BillRecord record : records) {
            System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(record.getDate()) + " " +
                    record.getAmount() + " " + record.getCategory() + " " + record.getRemark());
        }
    }

    public List<BillRecord> getIncomeRecords() {
        return incomeRecords;
    }

    public List<BillRecord> getExpenseRecords() {
        return expenseRecords;
    }

    public void queryRecords(List<BillRecord> records, String startDate, String endDate, String category) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar startCalendar = Calendar.getInstance();
        Calendar endCalendar = Calendar.getInstance();

        try {
            startCalendar.setTime(sdf.parse(startDate));
            endCalendar.setTime(sdf.parse(endDate));
        } catch (ParseException e) {
            System.out.println("日期格式错误，请重新输入！");
            return;
        }

        for (BillRecord record : records) {
            Calendar recordCalendar = Calendar.getInstance();
            recordCalendar.setTime(record.getDate());
            if (recordCalendar.after(startCalendar) && recordCalendar.before(endCalendar) &&
                    (category == null || record.getCategory().equals(category))) {
                System.out.println(new SimpleDateFormat("yyyy-MM-dd").format(record.getDate()) + " " +
                        record.getAmount() + " " + record.getCategory() + " " + record.getRemark());
            }
        }
    }

    public void setMonthlyBudget(double budget) {
        monthlyBudget = budget;
        System.out.println("月度预算已设置为：" + budget);
    }

    public double getRemainingBudget() {
        return monthlyBudget - totalExpenses;
    }

    public double getTotalIncome() {
        return incomeRecords.stream().mapToDouble(record -> record.getAmount()).sum();
    }

    public double getTotalExpense() {
        return expenseRecords.stream().mapToDouble(record -> record.getAmount()).sum();
    }

    public Map<String, Double> getCategoryAmount() {
        Map<String, Double> categoryAmount = new HashMap<>();
        for (BillRecord record : incomeRecords) {
            categoryAmount.merge(record.getCategory(), record.getAmount(), Double::sum);
        }
        for (BillRecord record : expenseRecords) {
            categoryAmount.merge(record.getCategory(), record.getAmount(), (a, b) -> a - b);
        }
        return categoryAmount;
    }
}//注释